<?php
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Controller\redirect;
use think\Session;

class Index extends Controller
{
    /*
        空操作
    */
    public function _empty()
    {
        echo "<script>alert('不存在的')</script>";
        return view('index/index');
    }

    /*
    后台登录模块
    */
    public function index()
    {
        $name = Session::get('name');     
        if(empty($name)){
            return view('Index/login');
        }else{
            return view('Index/index'); 
        }  
    }

    /* 
        登录逻辑判断
    */
    public function login(Request $Request,$name,$password)
    {
        // $post = $Request->post();

        // $name = $post['name'];
        // $password = $post['password'];
        if(!empty($name))
        { 
            Session::set('name',$name);
            Session::set('password',$password);            
            $admin = Db::name('admin')->where('name',$name)->find();
            // dump($admin['name']);die;
            if($name == $admin['name'] && $password == $admin['password'])
            {
                 return $this->redirect('admin/Index/index');
            }else{
                echo "<script>alert('账号密码不对');</script>";
                return view('Index/login');
            }
        }else{
            echo "<script>alert('不能为空');</script>";
            return view('Index/login');
        }
    }

    /* 
        退出登录
    */
    public function logout()
    {
        $name = Session::get('name'); 
        if(!empty($name))
        {
            Session::delete('name');
            Session::delete('password');  
            return view('Index/login');          
        }else{
            return view('Index/login');                      
        }
    }
}
